class ListNode {
    val: number
    next: ListNode | null
    constructor(val?: number, next?: ListNode | null) {
        this.val = (val === undefined ? 0 : val)
        this.next = (next === undefined ? null : next)
    }
}
/**
 * https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
 * 删除链表的重复元素
 * @param head 
 */
const deleteDuplicates = (head: ListNode | null): ListNode | null => {
    // 边界判断
    if (head === null || head.next === null) return head;

    const dummyHead = new ListNode();
    let prev = dummyHead,
        val = head.val,
        curr = head.next;
    
    

    return dummyHead.next;
};


const deleteDuplicates2 = (head: ListNode | null): ListNode | null => {
    // 边界判断
    if (head === null || head.next === null) return head;

    // 从头开始
    let val = head.val,
        prev = head,
        curr: ListNode | null = head.next;

    // 开始遍历
    while (curr !== null) {
        // 有重复的
        if (curr.val === val) {
            prev.next = curr.next;
            // 不用更改prev
        } else {
            // 更新val
            val = curr.val;
            prev = curr;
        }
        // 更改curr的指向
        curr = curr.next;
    }

    return head;
};

export {}